import { FC, useMemo } from 'react';
import { RouteComponentProps } from 'react-router'
import _ from 'lodash';
import moment from 'moment';
import { WithNormalRender } from "@ey-render/hoc-form";
import { getSubmitFormValues, getFormValues } from '@ey-utils/utils/formily-utils/getSubmitFormValues';
import { history } from 'umi';
import { uuidv4 } from '@ey-utils/utils/utils';
import { onFieldChange, onFieldInit, onFieldValueChange, onFormMount } from '@formily/core';
import api, { archivesUpdate } from './services';
import {useParams} from "react-router-dom";
import components from "@/components/schema-form-render/schema-components-registed/components-biz";

interface ArchivesMngEditProps extends RouteComponentProps {}

const ArchivesMngEdit: FC<ArchivesMngEditProps> = ({
    match
}) => {
    const { code} = useParams();

  const detailApi: any = useMemo(() => {
    return {
        remoteUrl: api.detail,
        method: 'POST',
        remoteParams: {
          code,
        },
        pageCode: 'ArcvManage_U',
    };
  }, [code]);
  const handleBack = () => {
    return history.back();
  };

  const BillFormLayout = useMemo(() => {
    return WithNormalRender({
        filedConfig: {},
        components: {
            ...components,
        },
    });
  }, []);

  // 提交
  const submit = async ({ form }: any) => {
    const formValues = await getSubmitFormValues(form);
    if (formValues) {
      const params = {
        ...formValues,
        code,
        pageCode: 'ArcvManage_U'
      };
      const res: any = await archivesUpdate(params);

      const { success } = res || {};

      if (success) {
        handleBack();
      }

      return res;
    }

    return {
      success: false,
    };
  };

  return (
    <BillFormLayout
      anchor
      type="NORMAL"
      {...detailApi}
      events={{
        submit,
      }}
      formConfigs={{
        effects: () => {
            onFormMount((form) => {
                // form.setFieldState('type', (field) => {
                //     field.componentProps = {
                //         ...field.componentProps,
                //         filterData: [
                //             "Form",
                //             "Attachments",
                //             "Invoices",
                //             "ElecApvlRec",
                //             "BankElecReceipt"
                //         ]
                //     }
                // })
            })
            onFieldValueChange('qzCode', (field, form) => {
                if (!field.modified) return;
                form.setValuesIn('qzName', field.inputValues?.[1]?.name);
            })
            onFieldValueChange('type', (field, form) => {
              form.setFieldState('ctgryCode', state => {
                state.required = field.value === 'ReportForms';
              })
            })
        },
      }}
      filedConfig={{}}
    />
  );
};

export default ArchivesMngEdit;
